package com.woniu.p2p.behindController;

import com.woniu.p2p.entity.Admin;
import com.woniu.p2p.service.AdminService;
import com.woniu.p2p.vo.Response;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @author 86152
 * @create 2020/8/14
 * @since 1.0.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    @RequestMapping("/login")
    @ResponseBody
    public Response login(String userName,String password){
        Response response = new Response();
        Admin admin = new Admin();
        admin.setAdminAccount(userName);
        admin.setAdminPassword(password);
        Subject subject = SecurityUtils.getSubject();
//        System.out.println(new SimpleHash("MD5", admin.getAdminPassword(), ""+ userName, 1024).toString());
        if(subject.isAuthenticated()){
            subject.logout();
        }
        UsernamePasswordToken token = new UsernamePasswordToken(""+userName, password);
        try{
            subject.login(token);
        }catch (UnknownAccountException e){
            response.setResult("error");
            response.setMsg("unknownAccount");
        }catch (IncorrectCredentialsException e){
            response.setResult("error");
            response.setMsg("incorrectCredentials");
        }catch (Exception e){
            response.setResult("error");
            response.setMsg("服务器错误");
        }
        if("error".equals(response.getResult()))
            subject.logout();
        return response;
    }

    @RequestMapping("/logout")
    @ResponseBody
    public Integer logout(){
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            subject.logout();
        }
        return 1;
    }

    @RequestMapping("/changepwd")
    @ResponseBody
    public String changepwd(String adminName, String oldpwd, String newpwd, HttpServletRequest request){
        String md5 = new SimpleHash("MD5", oldpwd, "" + adminName, 1024).toString();
        String md6 = new SimpleHash("MD5", newpwd, "" + adminName, 1024).toString();
        System.out.println(md5);
        System.out.println(md6);
        Admin admin = adminService.login(adminName);
        if(!admin.getAdminPassword().equals(md5)){
            return "密码错误！";
        }else{
            int row=adminService.changepwd(adminName,md6);
            if(row==1){
                Admin admin1=adminService.login(adminName);
                request.getSession().setAttribute("userInfo",admin1);
                return "修改成功！";
            }else{
                return "未知错误！";
            }
        }
    }

    @RequestMapping("/importhead")
    @ResponseBody
    public Integer importhead(@RequestParam("file") MultipartFile file,HttpServletRequest request){
        String path = "D:\\workspace_idea\\p2p_backstage\\src\\main\\resources\\static\\img";
        File uploads = new File(path);
        String fileName = file.getOriginalFilename();
        File newfile = new File(path, fileName);
        try {
            file.transferTo(newfile);
            Admin userInfo = (Admin) request.getSession().getAttribute("userInfo");
            String adminAccount = userInfo.getAdminAccount();
            String newimg="/img/"+file.getOriginalFilename();
            adminService.updateimgbyadminaccount(adminAccount,newimg);
            request.getSession().setAttribute("head","/img/"+file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
